local zishucup__shangbing = fk.CreateSkill{
  name = "zishucup__shangbing",
}

Fk:loadTranslationTable{
  ["zishucup__shangbing"] = "上兵",
  [":zishucup__shangbing"] = "你可以保留一张手牌，将其余手牌（至少一张）当<a href='shangbing_jink-href'>任意</a>基本牌或【无懈可击】使用或打出。<br>你失去唯一手牌时展示之，"..
  "若与你上次因“上兵”使用或打出的牌同名，你摸X张牌(X为你上次因“上兵”转化的牌数)。",

  ["shangbing_jink-href"] = "不包含闪【闪】",

  ["#zishucup__shangbing"] = "上兵：你可以选择手牌至仅剩一张手牌，将选择牌当基本牌或【无懈可击】使用或打出。",
  ["@zishucup__shangbing"] = "上兵",

  ["$zishucup__shangbing1"] = " ",
  ["$zishucup__shangbing2"] = " ",
}

zishucup__shangbing:addEffect("viewas", {
  anim_type = "support",
  pattern = ".|.|.|.|^glow__jink|basic;nullification",
  prompt = "#zishucup__shangbing",
  interaction = function(self, player)
    local all_names = Fk:getAllCardNames("b")
    table.insert(all_names, "nullification")
    table.removeOne(all_names, "glow__jink")
    local names = player:getViewAsCardNames(zishucup__shangbing.name, all_names)
    if #names > 0 then
      return UI.CardNameBox { choices = names, all_choices = all_names }
    end
  end,
  card_filter = function(self, player, to_select, selected)
    return table.contains(player:getCardIds("h"), to_select) and #selected < (player:getHandcardNum() - 1)
  end,
  view_as = function(self, player, cards)
    if not self.interaction.data or #cards ~= (player:getHandcardNum() - 1) then return end
    local card = Fk:cloneCard(self.interaction.data)
    card.skillName = zishucup__shangbing.name
    card:addSubcards(cards)
    return card
  end,
  before_use = function (self, player, use)
    player.room:setPlayerMark(player, "zishucup__shangbing_name", use.card.trueName)
    player.room:setPlayerMark(player, "zishucup__shangbing_num", #use.card.subcards)
    local record = {player:getMark("zishucup__shangbing_name"), player:getMark("zishucup__shangbing_num")}
    player.room:setPlayerMark(player, "@zishucup__shangbing", record)
  end,
  enabled_at_play = function (self, player)
    return #player:getCardIds("h") > 1
  end,
  enabled_at_response = function (self, player, response)
    return #player:getCardIds("h") > 1
  end,
})

zishucup__shangbing:addEffect(fk.AfterCardsMove, {
  anim_type = "support",
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(zishucup__shangbing.name) or not player:isKongcheng() then return end
    for _, move in ipairs(data) do
      if #move.moveInfo == 1 and move.from == player then
        for _, info in ipairs(move.moveInfo) do
          if info.fromArea == Card.PlayerHand then
            return true
          end
        end
      end
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local cards = {}
    for _, move in ipairs(data) do -- 可能会有陈到手牌打包到其他牌里面一起弃置的问题，需要单独摘出来展示
      if #move.moveInfo == 1 and move.from == player then
        for _, info in ipairs(move.moveInfo) do
          if info.fromArea == Card.PlayerHand then
            table.insert(cards, info.cardId)
          end
        end
      end
    end
    if #cards == 1 then
      player:showCards(Fk:getCardById(cards[1], true))
      if player:getMark("zishucup__shangbing_num") > 0 and player:getMark("zishucup__shangbing_name") ~= 0 and
      Fk:getCardById(cards[1], true).trueName == player:getMark("zishucup__shangbing_name") and not player.dead then
        player:drawCards(player:getMark("zishucup__shangbing_num"), zishucup__shangbing.name)
      end
    end
  end,
})

return zishucup__shangbing
